/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GlassLoading.java
 *
 * Created on Oct 3, 2011, 8:16:40 AM
 */
package azhar.view.com;

import azhar.engine.DetectModem;
import azhar.engine.MessageService;
import azhar.view.Main;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Al-Insan
 */
public class GlassLoading extends javax.swing.JPanel{

    private Thread th;
    private boolean block;
    private String port;
    private Main main;
    private DetectModem det;
    private MessageService mes;
    private MouseAdapter mouse = new MouseAdapter() {
    };
    private KeyAdapter key = new KeyAdapter() {
    };

    /** Creates new form GlassLoading */
    public GlassLoading(Main m) {
        super();
        main = m;
        initComponents();
    }

    public void setText(String t) {
        lb.setText(t);
    }

    public void serching() {
        det = new DetectModem(this);
    }

    public void startService() throws Exception {
        lb.setText("starting service");
        mes.startService();
        lb.setText("service was started");
        main.setService(mes);
        main.initMessage();
        main.setModemInfo(mes.getModemInfo());
        main.setBattery();
        Thread.sleep(1000);
        main.getGlassPane().setVisible(false);
        setBlock(false);
    }

    public void restartService() throws Exception{
        mes.restartService();
        main.setService(mes);
        main.setModemInfo(mes.getModemInfo());
        lb.setText("service was started");
        main.setBattery();
        Thread.sleep(1000);
        main.getGlassPane().setVisible(false);
        setBlock(false);
    }

    public void stopService() throws Exception {
        lb.setText("stopping service");
        main.releaseBattery();
        mes.stopService();
        main.setModemInfo("");
        main.setService(mes);
        lb.setText("service was stopped");
        Thread.sleep(1000);
        main.getGlassPane().setVisible(false);
        setBlock(false);
    }

    public void setPort(String port) {
        if (port != null) {
            try {
                lb.setText("device found");
                this.port = port;
                mes = new MessageService(this.port, main);
                Thread.sleep(1000);
                startService();
            } catch (Exception ex) {
                Logger.getLogger(GlassLoading.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            lb.setText("no device found");
            JOptionPane.showMessageDialog(main, "No device found, plug your modem device and restart application", "Error", JOptionPane.ERROR_MESSAGE);
            main.getGlassPane().setVisible(false);
            setBlock(false);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (block) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(new Color(240, 240, 240));
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.5F));
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }
    }

    public boolean isBlock() {
        return block;
    }

    public void setBlock(boolean b) {
        block = b;
        if (block) {
            addMouseListener(mouse);
            addKeyListener(key);
        } else {
            removeMouseListener(mouse);
            removeKeyListener(key);
        }
        repaint();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lb = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();

        setOpaque(false);
        setLayout(new java.awt.GridLayout(1, 0));

        jPanel2.setOpaque(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 366, Short.MAX_VALUE)
        );

        add(jPanel2);

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/azhar/view/images/loader.gif"))); // NOI18N
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 160, 120, 100));

        lb.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lb.setText("Searching Modem");
        lb.setToolTipText("");
        lb.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel1.add(lb, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 270, 240, 20));

        add(jPanel1);

        jPanel3.setOpaque(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 366, Short.MAX_VALUE)
        );

        add(jPanel3);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lb;
    // End of variables declaration//GEN-END:variables

}
